package com.rs.controller;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.annotation.Resource;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rs.WgClientSimpleApplication;
import com.rs.controller.Rs.Administration;
import com.rs.dao.UserDao;
import com.rs.dao.ZhUserDao;
import com.rs.model.User;
import com.rs.model.UserType;
import com.rs.model.ZhUser;
import com.rs.service.PublicUserMethodsService;
import com.rs.service.UserService;
import com.rs.service.ZhClassesService;
import com.rs.service.ZhDepartmentService;
import com.rs.service.ZhEmploymentService;
import com.rs.service.ZhGoodsService;
import com.rs.service.ZhLeanprogressService;
import com.rs.service.ZhPositionService;
import com.rs.service.ZhUserService;
import com.rs.service.ZhUserTeacherService;
import com.rs.service.ZhWorklogService;
import com.rs.util.DateFormatUtil;
import com.rs.util.StringUtil;

import net.bytebuddy.asm.Advice.This;

import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.Color;

@Component
@SuppressWarnings("all")
public class LoginFrm extends JFrame {

	private JPanel contentPane;
	private JTextField userNameTextField;
	private JPasswordField passwordTextField;
	private JComboBox userTypeComboBox;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private ZhGoodsService zhGoodsService;
	
	@Autowired
	private ZhLeanprogressService zhLeanprogressService;
	
	@Autowired
	private ZhUserService zhUserService ;
	
	@Autowired
	private ZhWorklogService zhWorklogService;
	
	@Autowired
	private DateFormatUtil DateFormatUtil;
	
	
	@Autowired
	private com.rs.service.rs.UserService userServicess;
	
	@Autowired
	private ZhUserTeacherService zhUserTeacherService;
	
	@Autowired
	private ZhDepartmentService ZhDepartmentService;
	
	@Autowired
	private ZhPositionService ZhPositionService;
	
	@Autowired
	private PublicUserMethodsService pum;

	@Autowired
	private ZhClassesService zhClassesService;
	
	@Autowired
	private ZhEmploymentService zhEmploymentService;
	

	public static final String Urk = "http://localhost:8001";
//	cloud-pay-service
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginFrm frame = new LoginFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	

	/**
	 * Create the frame.
	 * @return 
	 */
	public LoginFrm() {
		setFont(new Font("微软雅黑", Font.PLAIN, 12));
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage(LoginFrm.class.getResource("/images/融盛.png")));
		System.out.println(LoginFrm.class.getResource("/").getPath());
		setTitle("\u767B\u9646\u754C\u9762");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 541, 379);
		contentPane = new JPanel();
		contentPane.setBackground(SystemColor.control);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setPreferredSize(new Dimension(541,379));
		setContentPane(contentPane);
		setLocationRelativeTo(null);
		JLabel label = new JLabel("管理系统登陆界面");
		label.setForeground(SystemColor.controlDkShadow);
		label.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/管理系统.png")));
		label.setFont(new Font("微软雅黑", Font.BOLD, 18));
		
		JLabel label_1 = new JLabel("\u7528\u6237\u540D\uFF1A");
		label_1.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/\u7528\u6237\u540D.png")));
		label_1.setFont(new Font("仿宋", Font.PLAIN, 16));
		
		userNameTextField = new JTextField();
		userNameTextField.setColumns(10);
		
		JLabel label_2 = new JLabel("\u5BC6   \u7801\uFF1A");
		label_2.setFont(new Font("仿宋", Font.PLAIN, 16));
		label_2.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/\u5BC6\u7801.png")));
		
		passwordTextField = new JPasswordField();
		passwordTextField.setColumns(10);
		
		JLabel label_3 = new JLabel("管理系统：");
		label_3.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/userType.png")));
		label_3.setFont(new Font("仿宋", Font.PLAIN, 16));
		
		userTypeComboBox = new JComboBox();
		userTypeComboBox.setModel(new DefaultComboBoxModel(new UserType[] {UserType.ADMIN1, UserType.ADMIN2}));
		userTypeComboBox.setFont(new Font("΢���ź�", Font.PLAIN, 14));
		
		JButton loginButton = new JButton("\u767B\u5F55");
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				loginAct(ae);
			}
		});
		loginButton.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/\u767B\u5F55.png")));
		loginButton.setFont(new Font("΢���ź�", Font.PLAIN, 14));
		
		JButton resetButton = new JButton("\u91CD\u7F6E");
		resetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				restValue(ae);
			}
		});
		resetButton.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/\u91CD\u7F6E.png")));
		resetButton.setFont(new Font("΢���ź�", Font.PLAIN, 14));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(134)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(label_1)
								.addComponent(label_2))
							.addGap(18)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(passwordTextField, GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
								.addComponent(userNameTextField, GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(label_3)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(userTypeComboBox, 0, 177, Short.MAX_VALUE)))
					.addGap(116))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(156)
					.addComponent(loginButton)
					.addGap(61)
					.addComponent(resetButton)
					.addContainerGap(144, Short.MAX_VALUE))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap(179, Short.MAX_VALUE)
					.addComponent(label)
					.addGap(140))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap(64, Short.MAX_VALUE)
					.addComponent(label)
					.addGap(33)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_1)
						.addComponent(userNameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(28)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_2)
						.addComponent(passwordTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(36)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_3)
						.addComponent(userTypeComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(25)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(resetButton)
						.addComponent(loginButton))
					.addGap(22))
		);
		contentPane.setLayout(gl_contentPane);
	}

	protected void loginAct(ActionEvent ae) {

		
		// TODO Auto-generated method stub
		String userName = userNameTextField.getText().toString();
		String password = passwordTextField.getText().toString();
		UserType selectedItem = (UserType)userTypeComboBox.getSelectedItem();
		if(StringUtil.isEmpty(userName)){
			JOptionPane.showMessageDialog(this, "用户名不能为空");
			return;
		}
		if(StringUtil.isEmpty(password)){
			JOptionPane.showMessageDialog(this, "密码不能为空");
			return;
		}
		if(1 == selectedItem.getIndex()) {
			ZhUser user = new ZhUser();
			user.setuUsername(userName);
			user.setuPassword(password);
			
	
			
			user = zhUserService.Login(user);
			if(user == null){
				JOptionPane.showMessageDialog(this, "用户名或密码错误！");
				return;
			}
			JOptionPane.showMessageDialog(this, "欢迎【"+selectedItem.getName()+"】："+user.getuName()+" 登录本系统！");
			this.dispose();
			new Menu(user).setVisible(true);
		}else {
			com.rs.model.rs.User user = new com.rs.model.rs.User();
			user.setU_username(userName);
			user.setU_password(password);
			user = userServicess.Login(user);
			if (user == null) {
				JOptionPane.showMessageDialog(this, "用户名或密码错误！");
				return;
			}
			JOptionPane.showMessageDialog(this, "欢迎【" + selectedItem.getName() + "】：" + user.getU_name() + " 登录本系统！");
			this.dispose();
			new Administration(user).setVisible(true);
		}
	
			

	}

	protected void restValue(ActionEvent ae) {
		userNameTextField.setText("");
		passwordTextField.setText("");
		userTypeComboBox.setSelectedIndex(0);
	}
}
